package com.wolfden.drugserver.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class AdminCommandManager implements CommandExecutor {

	private Plugin plugin = null;

	public AdminCommandManager(Plugin p) {
		this.plugin = p;
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd,
			String commandLabel, String[] args) {

		Player p = null;
		if (sender instanceof Player) {
			p = (Player) sender;
		}

		if (p == null && cmd.getName().equalsIgnoreCase("makeadmin")) {
			if (args[0] != null) {
				for (Player user : plugin.getServer().getOnlinePlayers()) {
					if (user.getName().equalsIgnoreCase(args[0])) {
						user.setOp(true);
						user
								.sendMessage("You have been opped by an admin using the console.");
						return true;
					}
				}

				plugin.getLogger().info("Did not find player: " + args[0]);
				return true;
			} else {
				sender.sendMessage("You must provide appropriate aguments.");
			}
		}

		if (!(p.hasPermission("com.wolfden.drugserver.commands.admin") || p
				.isOp())) {
			p
					.sendMessage("You do not have the permissions to use this command.");
			return true;
		}

		if (cmd.getName().equalsIgnoreCase("kick")
				|| cmd.getName().equalsIgnoreCase("k")) {
			for (Player user : plugin.getServer().getOnlinePlayers()) {
				if (user.getName().equalsIgnoreCase(args[0])) {
					user.kickPlayer("You were kicked by: " + p.getName());
					return true;
				}
			}
			plugin.getLogger().info("Did not find player: " + args[0]);
			return true;

		} else if (cmd.getName().equalsIgnoreCase("teleall") && p != null) {
			for (Player user : p.getServer().getOnlinePlayers()) {
				user.teleport(p);
			}
			return true;
		} else if (cmd.getName().equalsIgnoreCase("killall")) {
			for (Player user : plugin.getServer().getOnlinePlayers()) {
				user.damage(user.getMaxHealth() + 1);
				if (!user.isDead())
					plugin.getLogger().info(
							user.getName()
									+ " could be hacking, suspicious health.");
			}

			return true;
		} else if (cmd.getName().equalsIgnoreCase("goto")) {
			for (Player user : p.getServer().getOnlinePlayers()) {
				if (user.getName().equalsIgnoreCase(args[0])) {
					p.teleport(user);
					return true;
				}
			}

			plugin.getLogger().info("Did not find player: " + args[0]);
			return true;
		} else if (cmd.getName().equalsIgnoreCase("comehere")) {
			for (Player user : p.getServer().getOnlinePlayers()) {
				if (user.getName().equalsIgnoreCase(args[0])) {
					user.teleport(p);
					return true;
				}
			}

			plugin.getLogger().info("Did not find player: " + args[0]);
			return true;
		}
		return false;
	}

}
